package pl.edu.agh.ki.ewywiadowka.client.windows;

import java.util.LinkedList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gen2.table.client.FixedWidthFlexTable;
import com.google.gwt.gen2.table.client.FixedWidthGrid;
import com.google.gwt.gen2.table.client.ScrollTable;
import com.google.gwt.gen2.table.client.AbstractScrollTable.SortPolicy;
import com.google.gwt.gen2.table.override.client.HTMLTable.RowFormatter;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;


import pl.edu.agh.ki.ewywiadowka.client.Const;
import pl.edu.agh.ki.ewywiadowka.client.EWywiadowka;
import pl.edu.agh.ki.ewywiadowka.client.Const.Error.ClassRemoveMsg;
import pl.edu.agh.ki.ewywiadowka.client.dto.ClassDTO;
import pl.edu.agh.ki.ewywiadowka.client.windows.secretary.ModifyClassWindow;

public class SearchClassWindow extends GenericWindow implements ClickHandler, TableListener {
	
	private ScrollPanel _mainPanel = new ScrollPanel();
	private DockPanel _dockPanel = new DockPanel();

	private ScrollTable _scrollTable = null;
	private Button _okButton = new Button("Wybierz klase");
	
	private FixedWidthGrid _dataTable;
	private FixedWidthFlexTable _headerTable;

	private int _selectedRow = -1;

	private SearchClassWindow _me;

	private LinkedList<ClassDTO> _classes = null; // bo na poczatku nie ma

	private int _mode = -1;

	public SearchClassWindow(EWywiadowka wywiadowka, int mode) {
		super(wywiadowka);
		
		_me = this;

		_mode = mode;
		
		_okButton.addClickHandler(this);
		
		_dataTable = new FixedWidthGrid(0, 3);
		_dataTable.setBorderWidth(1);

		_headerTable = new FixedWidthFlexTable();
		_headerTable.setBorderWidth(1);
		fillHeader();

		_scrollTable = new ScrollTable(_dataTable, _headerTable);
		_scrollTable.fillWidth();
		_scrollTable.setHeight("400px");

		_dockPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		_dockPanel.add(_scrollTable, DockPanel.CENTER);
		_dockPanel.add(_okButton, DockPanel.SOUTH);

		_mainPanel.add(_dockPanel);
		
		Const.eWywiadowkaService.getClasses(new AsyncCallback<LinkedList<ClassDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(LinkedList<ClassDTO> result) {
				_classes = result;

				_headerTable = new FixedWidthFlexTable();
				_headerTable.setBorderWidth(1);
				fillHeader();

				_dataTable = new FixedWidthGrid(_classes.size(), 5);
				_dataTable.setBorderWidth(1);
				repaintTable();

				_dockPanel.remove(_scrollTable);

				_scrollTable = new ScrollTable(_dataTable, _headerTable);
				_scrollTable.setHeight("400px");
				_scrollTable.setVisible(true);

				_dataTable.addTableListener(_me);
				_scrollTable.setSortPolicy(SortPolicy.DISABLED);

				_dockPanel.add(_scrollTable, DockPanel.CENTER);
				_scrollTable.fillWidth();
			}
			
		});
		
		this.initWidget(_mainPanel);
	}

	private void repaintTable() {
		int i = 0;
		for (ClassDTO clas : _classes)
			fillRow(i++, clas);
	}

	private void fillRow(int row, ClassDTO clas) {

		RowFormatter rowFormatter = _dataTable.getRowFormatter();

		if (row % 2 == 0)
			rowFormatter.addStyleName(row, "tableRowStyle1");
		else
			rowFormatter.addStyleName(row, "tableRowStyle2");

		_dataTable.setHTML(row, 0, clas.letter.toString());
		_dataTable.setHTML(row, 1, clas.annual.toString());
		_dataTable.setHTML(row, 2, clas.educator.toString());
	}

	private void fillHeader() {
		_headerTable.setHTML(0, 0, "Litera");
		_headerTable.setHTML(0, 1, "Rocznik");
		_headerTable.setHTML(0, 2, "Wychowawca");
	}

	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource() == _okButton) {
			if (_selectedRow == -1) {
				Window.alert("Prosze wybrac klase");
			} else {
				final ClassDTO clas = _classes.get(_selectedRow);
				
				_mainPanel.remove(_dockPanel);

				if (_mode == 1) { // modyfikacja
					_mainPanel.add(new ModifyClassWindow(getWywiadowka(),
							clas));
				} else if (_mode == 2) { // usuniecie
					Const.eWywiadowkaService.removeClass(clas,
							 new AsyncCallback<ClassRemoveMsg>() {

								@Override
								public void onFailure(Throwable caught) {
									Window.alert(caught.getMessage());
								}

								@Override
								public void onSuccess(ClassRemoveMsg result) {
									Window.alert(result.toString());
									_mainPanel.add(new SearchClassWindow(
											getWywiadowka(), 2));
								}

							});
				}
			}
		}
	}

	@Override
	public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
		RowFormatter rowFormatter = _dataTable.getRowFormatter();
		if (_selectedRow != -1) {
			if (_selectedRow % 2 == 0)
				rowFormatter.setStyleName(_selectedRow, "tableRowStyle1");
			else
				rowFormatter.setStyleName(_selectedRow, "tableRowStyle2");
		}
		_selectedRow = row;
		rowFormatter.setStyleName(row, "clickedTableRowStyle");
	}



}
